package cate.game.event.hero;

import cate.common.table.d.GDFunc;
import cate.game.activity.base.core.RoleFuncActivitySingle;
import cate.game.activity.commonstarup.CommonStarUpGiftActivity;
import cate.game.activity.customgrowth.CustomGrowthActivity;
import cate.game.activity.godtree.GodTreeActivity;
import cate.game.activity.newhero.NewHeroActivity;
import cate.game.activity.specall.SpeCallActivity;
import cate.game.event.GameEventListener;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;

import java.util.List;


/**
 * 英雄星级改变会触发，包括英雄被献祭，从xx星变为0星
 */
public class HeroStarChangeListener implements GameEventListener<HeroStarChangeEvent> {
    @Override
    public void onEvent(HeroStarChangeEvent gameEvent) {
        Role role = gameEvent.role;
        Hero hero = role.getBag().hero.getItem(gameEvent.heroUid);
        //如果新的星级比旧的星级高，包括了新增英雄（抽卡，碎片合成），升星
        if (gameEvent.newStar > gameEvent.oldStar) {
            //新获得的英雄
            role.getRes().heroIntroduction.addHero(hero.tid, hero.getTpl(role.getGame()).protoId);
            role.getHistory().action.heroCollection(gameEvent.newStar);
            role.getActivity().getEnableList(CommonStarUpGiftActivity.class).forEach(single -> single.updateAvailable(gameEvent.heroUid));
            role.getActivity().getEnableList(GodTreeActivity.class).forEach(RoleFuncActivitySingle::checkOpen);
            role.getActivity().getEnableList(SpeCallActivity.class).forEach(a -> a.updateAvailable(gameEvent.heroUid));
            role.getRes().magicContract.checkOpen();
            //定制成长
            CustomGrowthActivity growthSingle = role.getActivity().getEnableActivitySingle(GDFunc.CUSTOM_GROWTH);
            if (growthSingle != null) {
                growthSingle.updateAvailable(gameEvent.heroUid);
            }
            //
            NewHeroActivity newHeroSingle = role.getActivity().getEnableActivitySingle(GDFunc.NEW_HERO);
            if (newHeroSingle != null) {
                newHeroSingle.updateAvailable(gameEvent.heroUid);
            }
            role.getHistory().action.campHeroStarUp(hero.getTpl(role.getGame()).camp, gameEvent.newStar);
        }
    }
}
